﻿#region Imports
using System;
using System.ComponentModel;
#endregion

namespace BulkRenamer.Util.Extensions
{
    public static class AttributeExtensions
    {
        // Org extn. method
        // RKD : Test PENDING
        public static TAttrib GetAttribute<TObject, TAttrib>(this TObject value)
            where TObject : class, new()
            where TAttrib : Attribute
        {
            //FieldInfo field = typeof(TClass).GetField(propertyName);
            //if (field != null)
            //{
            //    TAttrib attr = Attribute.GetCustomAttribute(field, typeof(TAttrib)) as TAttrib;
            //    if (attr != null) return attr.GetType().GetProperty(propertyName).GetValue(attr, null);
            //}

            if (Utilities.IsNull(value)) value = new TObject { };
            AttributeCollection attributes = TypeDescriptor.GetAttributes(typeof(TObject));
            TAttrib myAttribute = (TAttrib)attributes[typeof(TAttrib)];
            return myAttribute;
        }

        // Param-less overload method
        // RKD : Test PENDING
        public static TAttrib GetAttribute<TObject, TAttrib>()
            where TObject : class, new()
            where TAttrib : Attribute
        {
            TObject value = new TObject { };
            return value.GetAttribute<TObject, TAttrib>();
        }

        //// Class object param overload method
        //public static TAttrib GetAttribute<TObject, TAttrib>(TObject value)
        //    where TObject : class, new()
        //    where TAttrib : Attribute
        //{
        //    if (Utilities.IsNull(value)) value = new TObject { };
        //    return value.GetAttribute<TObject, TAttrib>();
        //}

        // Org extn. method
        // RKD : Test PENDING
        public static string GetClassDescription<TClass>(this TClass value)
           where TClass : class, new()
        {
            if (Utilities.IsNull(value)) value = new TClass { };
            string desc = Convert.ToString(value.GetAttributeProperty<TClass, DescriptionAttribute>("Description"));
            return !desc.IsNullOrEmpty() ? desc : "N/A";
        }

        // Param-less overload method
        // RKD : Test PENDING
        public static string GetClassDescription<TClass>()
           where TClass : class, new()
        {
            TClass value = new TClass { };
            return value.GetClassDescription<TClass>();
        }

        //// Class object param overload method
        //public static string GetClassDescription<TClass>(TClass value)
        //where TClass : new()
        //{
        //    if (Utilities.IsNull(value)) value = new TClass { };
        //    return value.GetClassDescription<TClass>();
        //}

        // Org extn. method
        // RKD : Test PENDING
        public static object GetAttributeProperty<TClass, TAttrib>(this TClass value, string propertyName)
            where TClass : class, new()
            where TAttrib : Attribute
        {
            if (Utilities.IsNull(value)) value = new TClass { };
            TAttrib myAttribute = value.GetAttribute<TClass, TAttrib>();
            return myAttribute.GetType().GetProperty(propertyName).GetValue(myAttribute, null);
        }

        // Param-less overload method
        // RKD : Test PENDING
        public static object GetAttributeProperty<TClass, TAttrib>(string propertyName)
            where TClass : class, new()
            where TAttrib : Attribute
        {
            TClass value = new TClass { };
            return value.GetAttributeProperty<TClass, TAttrib>(propertyName);
        }

        //// Class object param overload method
        //public static object GetPropertyAttribute<TClass, TAttrib>(TClass value, string propertyName)
        //    where TClass : class, new()
        //    where TAttrib : Attribute
        //{
        //    if (Utilities.IsNull(value)) value = new TClass { };
        //    return value.GetPropertyAttribute<TClass, TAttrib>(propertyName);
        //}
    }
}
